from fastapi import APIRouter
from fastapi import Request
from service.ReferService import ReferService
from util.MyExceptionHandler import MyExceptionHandler
import inspect
import json
from util import Logger
from StatusCode import StatusCode

router = APIRouter(tags=["转介申请单路由"])


@router.get("/refer/list")
def getReferList(request: Request):
    try:
        json_data = dict(request.query_params)
        data = ReferService().getReferList(json_data)
        response = {
            "code": StatusCode.RESPONSE_SUCCESS,
            "data": data.to_dict(orient="records"),
            "total": data.shape[0],
        }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.get("/refer/info")
def getReferInfo(request: Request):
    try:
        json_data = dict(request.query_params)
        data = ReferService().getReferInfo(json_data)
        if data == StatusCode.RESPONSE_FAIL:
            response = {
                "code": StatusCode.RESPONSE_FAIL,
                "msg": "该转介申请单不存在或已被锁定",
            }
        else:
            response = {
                "code": StatusCode.RESPONSE_SUCCESS,
                "data": data,
            }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.post("/refer/update")
async def updateRefer(request: Request):
    try:
        json_data = await request.json()
        data = ReferService().updateRefer(json_data)
        if data == StatusCode.RESPONSE_FAIL:
            response = {
                "code": StatusCode.RESPONSE_FAIL,
                "msg": "该转介申请单不存在",
            }
        else:
            response = {
                "code": StatusCode.RESPONSE_SUCCESS,
            }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.post("/refer/add")
async def addRefer(request: Request):
    try:
        json_data = await request.json()
        data = ReferService().addRefer(json_data)
        if data == StatusCode.RESPONSE_EXIST:
            response = {
                "code": StatusCode.RESPONSE_EXIST,
                "msg": "该转介申请单已存在",
            }
        else:
            response = {
                "code": StatusCode.RESPONSE_SUCCESS,
            }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.get("/refer/del")
def delRefer(request: Request):
    try:
        json_data = dict(request.query_params)
        data = ReferService().delRefer(json_data)
        response = {
            "code": StatusCode.RESPONSE_SUCCESS,
        }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.get("/refer/todo")
def checkTodoRefer(request: Request):
    try:
        json_data = dict(request.query_params)
        data = ReferService().checkTodoRefer(json_data)
        response = {
            "code": StatusCode.RESPONSE_SUCCESS,
            "data": data.to_dict(orient="records"),
            "total": data.shape[0],
        }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response


@router.get("/refer/confirm")
def confirmRefer(request: Request):
    try:
        json_data = dict(request.query_params)
        data = ReferService().confirmRefer(json_data)
        response = {
            "code": StatusCode.RESPONSE_SUCCESS,
        }
    except Exception as e:
        response = MyExceptionHandler().postException(
            inspect.currentframe().f_code.co_name, e
        )
    return response
